package com.yzf.wisecoder.controller;

import com.yzf.wisecoder.wisecodercommon.response.Response;
import com.yzf.wisecoder.wisecodercommon.utils.BeanCopyUtils;
import com.yzf.wisecoder.wisecodercommon.utils.ValidatorUtils;
import com.yzf.wisecoder.wisecodercommon.vo.request.TemStoreReq;
import com.yzf.wisecoder.wisecodercommon.vo.response.TemStoreResp;
import com.yzf.wisecoder.wisecoderservice.service.TemStoreService;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.TemStoreEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author jianghuiyong
 * @email jianghuiyong@yunzhangfang.com
 * @date 2020-05-12 10:20:48
 */
@RestController
@RequestMapping("sys/temstore")
@AllArgsConstructor
@Api(value = "模板仓库控制", tags = "模板仓库控制")
public class TemStoreController {

    private final TemStoreService temStoreService;

    /**
     * 查询模板库列表
     *
     * @param params
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(value = "temName", name = "模板名称(用户模糊查询)", dataType = "String")
    })
    @GetMapping("/list")
    @ApiOperation(value = "查询模板库列表")
    public Response<List<TemStoreResp>> page(@RequestParam Map<String, Object> params) {
        List<TemStoreEntity> list = temStoreService.queryList(params);
        List<TemStoreResp> temStoreResps = BeanCopyUtils.copyList(list, TemStoreResp.class);
        return Response.buildSuccessResponseWithData(temStoreResps);
    }


    /**
     * 查询模板信息
     *
     * @param temId
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "查询模板信息")
    public Response<TemStoreResp> info(@RequestParam Long temId) {
        TemStoreEntity temStore = temStoreService.getById(temId);
        TemStoreResp temStoreResp = new TemStoreResp();
        BeanUtils.copyProperties(temStore, temStoreResp);
        return Response.buildSuccessResponseWithData(temStoreResp);
    }

    /**
     * 保存或修改模板信息
     *
     * @param temStoreReq
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "保存或修改模板信息")
    public Response save(@RequestBody TemStoreReq temStoreReq) {
        ValidatorUtils.validateEntity(temStoreReq);
        TemStoreEntity temStoreEntity = new TemStoreEntity();
        BeanUtils.copyProperties(temStoreReq, temStoreEntity);
        temStoreService.saveOrUpdateTemStore(temStoreEntity);

        return Response.buildSuccessResponse();
    }

    /**
     * 删除模板信息
     *
     * @param temIds
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除模板")
    public Response delete(@RequestBody List<Long> temIds) {
        temStoreService.removeByIds(temIds);

        return Response.buildSuccessResponse();
    }

}
